﻿Public Class oDocLinePropertyLink
    Public Function UpdateJsonDocLinePropertyLink(ByVal ItemID As String, ByVal propertyDetailID As String, _
                                ByVal propertyLinkID As String, ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {ItemID, propertyDetailID}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" IF NOT EXISTS(SELECT 1 FROM oDocLinePropertyLink WHERE [LineNum] = @Param1 AND PropertyDetailID = @Param2)")
                    query.Append(" INSERT INTO [oDocLinePropertyLink]")
                    query.Append("  ([LineNum]")
                    query.Append(" ,[PropertyDetailID])")
                    query.Append(" VALUES (@Param1,@Param2)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("DELETE  [oDocLinePropertyLink] WHERE ")
                    query.Append("  [LineNum] = @Param1")
                    query.Append("  AND [PropertyDetailID] = @Param2")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {ItemID, propertyDetailID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonDocLinePropertyLink(ByVal dataBaseName As String, ByVal propertyLinkID As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {propertyLinkID}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oDocLinePropertyLink]")
                query.Append("  WHERE oItemPropertyLink.PropertyLinkID =  @Param1")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllDocLinePropertyLink(ByVal dataBaseName As String, ByVal companyCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oDocLinePropertyLink")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            Dim query As New StringBuilder
            If errMsg.Length = 0 Then
                query.Append("SELECT * FROM oDocLinePropertyLink T0 JOIN oDocLinePropertyDetail T1 ON T0.PropertyDetailID = T1.PropertyDetailID")
                query.Append(" JOIN oDocLine T2 ON T2.LineNum = T0.LineNum ")
                query.Append(" JOIN oUser T3 ON T2.CreatedUserID = T3.UserID ")
                query.Append(" JOIN oCompany T4 ON T2.CompanyCode = T4.CompanyCode ")
                query.Append(" WHERE T2.CompanyCode  =  @Param1")
                query.Append("  ORDER BY T0.PropertyLinkID DESC")
                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {companyCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetAllDocLinePropertyLinkByItem(ByVal dataBaseName As String, ByVal propertyID As String, ByVal itemID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oDocLinePropertyLink")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            Dim query As New StringBuilder
            If errMsg.Length = 0 Then
                query.Append("  SELECT * FROM  oDocLinePropertyDetail T2 LEFT JOIN ( SELECT T1.* FROM oDocLine T0 LEFT JOIN oDocLinePropertyLink T1")
                query.Append("  ON T0.LineNum = T1.LineNum")
                query.Append("  WHERE T0.LineNum = @Param1)  T1  ON T2.PropertyDetailID =  T1.PropertyDetailID   ")
                query.Append(" WHERE T2.PropertyID = @Param2")
                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {itemID, propertyID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
